<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>入门-JAVA之路 |  朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-入门-JAVA之路"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  入门-JAVA之路
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/05/10/%E5%85%A5%E9%97%A8-JAVA%E4%B9%8B%E8%B7%AF/" class="article-date">
  <time datetime="2023-05-10T01:00:57.000Z" itemprop="datePublished">2023-05-10</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java/">Java</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">9.6k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">46 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h1 id="JAVA个人环境准备"><a href="#JAVA个人环境准备" class="headerlink" title="JAVA个人环境准备"></a>JAVA个人环境准备</h1><h2 id="下载IDEA2021-2-3"><a href="#下载IDEA2021-2-3" class="headerlink" title="下载IDEA2021.2.3"></a>下载IDEA2021.2.3</h2><p><a target="_blank" rel="noopener" href="https://www.jetbrains.com/idea/download/other.html">https://www.jetbrains.com/idea/download/other.html</a>  靠下位置有，已经下载到“下载目录”</p>
<h2 id="下载JDK8"><a href="#下载JDK8" class="headerlink" title="下载JDK8"></a>下载JDK8</h2><p><a target="_blank" rel="noopener" href="https://www.oracle.com/java/technologies/downloads/#java8">https://www.oracle.com/java/technologies/downloads/#java8</a>  考下位置有，已经下载到“下载目录”</p>
<h2 id="安装JDK8"><a href="#安装JDK8" class="headerlink" title="安装JDK8"></a>安装JDK8</h2><ul>
<li><strong>卸载jdk18步骤：</strong></li>
</ul>
<ol>
<li>删除目录 D:\install\jdk-18； 我这里直接改名： D:\install\jdk-18太新了不用</li>
<li>删除JAVA_HOME</li>
<li>删除PATH  (JDK18的只有jdk&#x2F;bin目录，没有jre&#x2F;并目录)</li>
</ol>
<ul>
<li><strong>安装jdk8步骤：</strong></li>
</ul>
<ol>
<li>安装软件到 D:\install\jdk8</li>
<li>添加 JAVA_HOME  D:\install\jdk8</li>
<li>添加PATH  D:\install\jdk8\bin  D:\install\jdk8\jre\bin</li>
<li>删除PATH C:\Program Files (x86)\Common Files\Oracle\Java\javapath</li>
</ol>
<h2 id="安装破解IDEA2021-2-3"><a href="#安装破解IDEA2021-2-3" class="headerlink" title="安装破解IDEA2021.2.3"></a>安装破解IDEA2021.2.3</h2><ol>
<li>参考破解网址： <a target="_blank" rel="noopener" href="http://www.itmind.net/10705.html">http://www.itmind.net/10705.html</a></li>
<li>卸载IDEA2022</li>
<li>安装IDEA2021.2.3.exe</li>
<li>安装目录： D:\install\IDEA2021.2.3  (勾选桌面图标 和 .java)</li>
<li>启动IDEA</li>
<li>Start trial ： 注册JETBRAINS账号使用github账号wangwei129, WANG WEI 密码 a&amp;89Uiojkl</li>
<li>新建java项目 ：参考 <a target="_blank" rel="noopener" href="http://www.itmind.net/5859.html">http://www.itmind.net/5859.html</a></li>
<li>然后依次点击菜单 <code>Help -&gt; Edit Custom VM Options</code> 来通过 IDEA 修改 <code>idea.vmoptions</code> 文件, 在末尾添加解补丁:</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">-javaagent:D:\\IDEA30TRIAL\FineAgent.jar</span><br></pre></td></tr></table></figure>

<p>​	9. 补丁FineAgent.jar可获取 :</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">补丁安装教程：http://www.itmind.net/10701.html</span><br><span class="line">补丁网盘链接: https://pan.baidu.com/s/1E7URZ_bTe3XTvyUOEBsYIw 提取码: cv5c</span><br><span class="line">备用链接: https://pan.baidu.com/s/1gERUKH934jiZekIligoP-Q 提取码: 3v6a</span><br><span class="line">长按扫描下方二维码加我微信，私信我 888 拉你入群，第一时间获取更新信息~~</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">补丁已经保存到我的网盘2022目录下</span></span><br></pre></td></tr></table></figure>

<pre><code>10. 关闭IDEA
</code></pre>
<ol start="11">
<li><p>启动IDEA</p>
</li>
<li><p>现在注册码注册</p>
</li>
<li><p>把中D:\IDEA30TRIAL\ActivationCode.txt的注册码复制填进去</p>
</li>
<li><p>破解完成。</p>
<p>注明：自动导入包和删包： 文件-设置-编辑-常规-自动导入</p>
</li>
</ol>
<h2 id="下载安装Tomcat8"><a href="#下载安装Tomcat8" class="headerlink" title="下载安装Tomcat8"></a>下载安装Tomcat8</h2><p><a target="_blank" rel="noopener" href="https://tomcat.apache.org/download-80.cgi">https://tomcat.apache.org/download-80.cgi</a></p>
<p>解压即安装：安装目录  D:\install\apache-tomcat-8.5.78-windows-x64\apache-tomcat-8.5.78</p>
<p>启动 start.bat</p>
<p>停止 stop.bat</p>
<p>把你的war包放在 D:\install\apache-tomcat-8.5.78-windows-x64\apache-tomcat-8.5.78\webapps 目录下即可</p>
<blockquote>
<p>1、tomcat窗口中内容中文<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=%E4%B9%B1%E7%A0%81&spm=1001.2101.3001.7020">乱码</a>解决：<br>找到tomcat下conf文件夹下的logging.<a target="_blank" rel="noopener" href="https://so.csdn.net/so/search?q=properties&spm=1001.2101.3001.7020">properties</a>文件，打开后找到<br>java.util.logging.ConsoleHandler.encoding &#x3D; UTF-8，将其修改为<br>java.util.logging.ConsoleHandler.encoding &#x3D; GBK </p>
</blockquote>
<p>注明： 直接把这个安装目录上传到linux上，页可以使用，使用sh启停。</p>
<hr>
<h1 id="JAVA-web项目实战流程"><a href="#JAVA-web项目实战流程" class="headerlink" title="JAVA web项目实战流程"></a>JAVA web项目实战流程</h1><h2 id="新建web项目"><a href="#新建web项目" class="headerlink" title="新建web项目"></a>新建web项目</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">1. File - New - Project</span><br><span class="line">2. Java - Next</span><br><span class="line">3. 不选择任何模板</span><br><span class="line">4. 给项目取名字webdemo</span><br><span class="line">5. 选中创建好的项目 - 右键 - Add Framework Support - Java Web</span><br><span class="line">6. Run - Edit Configurations - Edit configuration templates... - Tomcat Server - Local  - Application Server: - Configure... - 选择Tomcat的安装目录</span><br><span class="line">7. Run - Edit Configurations - + - Tomcat Server -  选择打包内容 修改打包名(即是war包名或路径名)</span><br><span class="line">8. 运行Web项目</span><br></pre></td></tr></table></figure>



<h2 id="第一个Servlet页面"><a href="#第一个Servlet页面" class="headerlink" title="第一个Servlet页面"></a>第一个Servlet页面</h2><h3 id="1-添加tomcat的jar包"><a href="#1-添加tomcat的jar包" class="headerlink" title="1. 添加tomcat的jar包"></a>1. 添加tomcat的jar包</h3><blockquote>
<ol>
<li><p>右键工程</p>
</li>
<li><p>Open module setging 模块设置</p>
</li>
<li><p>Libraries</p>
</li>
<li><p>+java</p>
</li>
<li><p>打开目录D:\install\apache-tomcat-8.5.78-windows-x64\apache-tomcat-8.5.78\lib</p>
</li>
<li><p>添加jsp-api.jar和servlet-api.jar两个包</p>
</li>
<li><p>起名Name：lib</p>
</li>
<li><p>Apply - OK</p>
</li>
</ol>
</blockquote>
<h3 id="2-src"><a href="#2-src" class="headerlink" title="2. src"></a>2. src</h3><blockquote>
<p>新建包： com.trey.servlet</p>
<p>新建类： Servlet001.java</p>
<p>继承：     extends HttpServlet</p>
<p>重写方法：  doGet  &#x2F;  doPost</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Servlet001</span> <span class="keyword">extends</span> <span class="title class_">HttpServlet</span> &#123;</span><br><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">     resp.setContentType(<span class="string">&quot;text/html&quot;</span>);</span><br><span class="line">     <span class="type">PrintWriter</span> <span class="variable">writer</span> <span class="operator">=</span> resp.getWriter();</span><br><span class="line">     writer.println(<span class="string">&quot;&lt;html&gt;&lt;body&gt;&quot;</span>);</span><br><span class="line">     <span class="type">String</span> <span class="variable">s</span> <span class="operator">=</span> <span class="string">&quot;Hello servlet Web&quot;</span>;</span><br><span class="line">     writer.println(s);</span><br><span class="line">     writer.println(<span class="string">&quot;&lt;/body&gt;&lt;/html&gt;&quot;</span>);</span><br><span class="line"> &#125;</span><br><span class="line"></span><br><span class="line"> <span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">     <span class="built_in">super</span>.doPost(req, resp);</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="3-web-xml"><a href="#3-web-xml" class="headerlink" title="3. web.xml"></a>3. web.xml</h3><blockquote>
<ol>
<li><servlet>
</li>
<li><servlet-mapping></li>
</ol>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version=<span class="string">&quot;1.0&quot;</span> encoding=<span class="string">&quot;UTF-8&quot;</span>?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">web-app</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xmlns:xsi</span>=<span class="string">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">xsi:schemaLocation</span>=<span class="string">&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd&quot;</span></span></span><br><span class="line"><span class="tag">         <span class="attr">version</span>=<span class="string">&quot;4.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>servlet001<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.trey.servlet.Servlet001<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>servlet001<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/hello<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">    </span><br><span class="line"><span class="tag">&lt;/<span class="name">web-app</span>&gt;</span></span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="4-启动项目"><a href="#4-启动项目" class="headerlink" title="4. 启动项目"></a>4. 启动项目</h3><h3 id="5-访问网站"><a href="#5-访问网站" class="headerlink" title="5. 访问网站"></a>5. 访问网站</h3><blockquote>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/webdemo_war/">http://localhost:8080/webdemo_war/</a>            项目主页</p>
<p><a target="_blank" rel="noopener" href="http://localhost:8080/webdemo_war/hello">http://localhost:8080/webdemo_war/hello</a>   第一个servlet页</p>
</blockquote>
<h1 id="打包IDEA项目成war包"><a href="#打包IDEA项目成war包" class="headerlink" title="打包IDEA项目成war包"></a>打包IDEA项目成war包</h1><h2 id="打包和发布"><a href="#打包和发布" class="headerlink" title="打包和发布"></a>打包和发布</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">1、进入Project Structre </span><br><span class="line">	（文件 - 项目结构）</span><br><span class="line">2、找到Atrtifacts模块 —&gt; 点击左上角添加 —&gt; Web Application: Archive —&gt; For &quot; &quot;</span><br><span class="line">	（工件 - 添加 - web应用程序：归档 - for&quot;Web名称&quot;）</span><br><span class="line">3、指定名称和输出路径；点击下方 + 号，选择 Directory Content</span><br><span class="line">	（打包取名： webdemo）</span><br><span class="line">	（输出路径： D:\go\src\webdemo\out\artifacts\webdemo）</span><br><span class="line">	（Directory Content好像不用做）</span><br><span class="line">4、选择项目的 WebRoot目录 ，点击 ok</span><br><span class="line">	（Directory Content好像不用做）</span><br><span class="line">5、回到主页面，点击 Build -&gt; 选择 Build Artifacts -&gt; 选择刚刚生成的artifact -&gt; 点击 build</span><br><span class="line">6、等待build结束，在指定的输出路径中拿到war包</span><br><span class="line">	（D:\go\src\webdemo\out\artifacts\webdemo\webdemo.war）</span><br><span class="line">7、把war包上传到tomcat的webapps目录即可</span><br></pre></td></tr></table></figure>



<hr>
<h1 id="Servlet-JSP-JDBC综合案例"><a href="#Servlet-JSP-JDBC综合案例" class="headerlink" title="Servlet+JSP+JDBC综合案例"></a>Servlet+JSP+JDBC综合案例</h1><h2 id="新建WEB项目"><a href="#新建WEB项目" class="headerlink" title="新建WEB项目"></a>新建WEB项目</h2><p>​	如上</p>
<h2 id="添加数据库驱动包"><a href="#添加数据库驱动包" class="headerlink" title="添加数据库驱动包"></a>添加数据库驱动包</h2><blockquote>
<ol>
<li>WEB-INF  下新建目录 lib</li>
<li>把D:\jdbc\mysql\mysql-connector-java-5.1.42-bin.jar 复制到 lib</li>
<li>后续动作如上（添加tomcat的jar包）。</li>
</ol>
</blockquote>
<h2 id="测试一下jdbc"><a href="#测试一下jdbc" class="headerlink" title="测试一下jdbc"></a>测试一下jdbc</h2><blockquote>
<ol>
<li>新建包 com.trey.testjdbc</li>
<li>新建类 TestJdbc</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.testjdbc;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.Connection;</span><br><span class="line"><span class="keyword">import</span> java.sql.DriverManager;</span><br><span class="line"><span class="keyword">import</span> java.sql.SQLException;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">TestJdbc</span> &#123;</span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">     <span class="keyword">try</span> &#123;</span><br><span class="line">         Class.forName(<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>);</span><br><span class="line">         <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql://1.1.1.3:3306/bookdb?useUnicode=true&amp;characterEncoding=UTF-8&quot;</span>;</span><br><span class="line">         <span class="type">String</span> <span class="variable">user</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">         <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;Passwd123&quot;</span>;</span><br><span class="line">         <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> DriverManager.getConnection(url,user,password);</span><br><span class="line">         System.out.println(connection);</span><br><span class="line">     &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">         e.printStackTrace();</span><br><span class="line">     &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">         e.printStackTrace();</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li>运行测试类，能够正常拿到connection没有任何报错，证明jdbc正常。</li>
</ol>
</blockquote>
<h2 id="在mysql中创建表user"><a href="#在mysql中创建表user" class="headerlink" title="在mysql中创建表user"></a>在mysql中创建表user</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">select * from mysql.user;</span></span><br><span class="line"><span class="comment">flush privileges;</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> `<span class="keyword">user</span>`;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> `<span class="keyword">user</span>`  (</span><br><span class="line">  `id` <span class="type">int</span>(<span class="number">11</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> AUTO_INCREMENT,</span><br><span class="line">  `name` <span class="type">varchar</span>(<span class="number">9</span>) <span class="type">CHARACTER</span> <span class="keyword">SET</span> utf8 <span class="keyword">COLLATE</span> utf8_general_ci <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  `age` <span class="type">int</span>,</span><br><span class="line">  `register` datetime,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (`id`) <span class="keyword">USING</span> BTREE </span><br><span class="line">) ENGINE <span class="operator">=</span> InnoDB AUTO_INCREMENT <span class="operator">=</span> <span class="number">5</span> <span class="type">CHARACTER</span> <span class="keyword">SET</span> <span class="operator">=</span> utf8 <span class="keyword">COLLATE</span> <span class="operator">=</span> utf8_general_ci ROW_FORMAT <span class="operator">=</span> Compact</span><br><span class="line">;</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name,age,register) <span class="keyword">values</span>(<span class="string">&#x27;张三&#x27;</span>,<span class="number">36</span>,<span class="string">&#x27;2022-05-22 08:00:00&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name,age,register) <span class="keyword">values</span>(<span class="string">&#x27;李四&#x27;</span>,<span class="number">36</span>,<span class="string">&#x27;2022-05-22 08:00:00&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name,age,register) <span class="keyword">values</span>(<span class="string">&#x27;王五&#x27;</span>,<span class="number">36</span>,<span class="string">&#x27;2022-05-22 08:00:00&#x27;</span>);</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> <span class="keyword">user</span>;</span><br></pre></td></tr></table></figure>

<h2 id="包类规划"><a href="#包类规划" class="headerlink" title="包类规划"></a>包类规划</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.entity;      <span class="comment">//实体包   Student 				 //实体类</span></span><br><span class="line"><span class="keyword">package</span> com.trey.repository;  <span class="comment">//储藏包   StudentRepository      //储藏类</span></span><br><span class="line"><span class="keyword">package</span> com.trey.servlet;     <span class="comment">//控制包	  StudentServlet         //控制类</span></span><br><span class="line"><span class="keyword">package</span> com.trey.filter;      <span class="comment">//过滤器包  CharacterFilter       //过滤器类</span></span><br><span class="line"><span class="keyword">package</span> com.trey.entity;  <span class="comment">//实体类</span></span><br><span class="line"><span class="keyword">package</span> com.trey.entity;  <span class="comment">//实体类</span></span><br></pre></td></tr></table></figure>

<h2 id="java-查询-mysql"><a href="#java-查询-mysql" class="headerlink" title="java 查询 mysql"></a>java 查询 mysql</h2><p>注明：自动生成getter和setter的快捷键是alt+insert，然后选中所有字段即可。</p>
<h4 id="实体类"><a href="#实体类" class="headerlink" title="实体类"></a>实体类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.entity;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Date;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Student</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> Integer id;</span><br><span class="line">    <span class="keyword">private</span> String  name;</span><br><span class="line">    <span class="keyword">private</span> Double age;</span><br><span class="line">    <span class="keyword">private</span> Date register;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Integer <span class="title function_">getId</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setId</span><span class="params">(Integer id)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Double <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(Double age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> Date <span class="title function_">getRegister</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> register;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setRegister</span><span class="params">(Date register)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.register = register;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">Student</span><span class="params">(Integer id, String name, Double age, Date register)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.id = id;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">        <span class="built_in">this</span>.register = register;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;Student&#123;&quot;</span> +</span><br><span class="line">                <span class="string">&quot;id=&quot;</span> + id +</span><br><span class="line">                <span class="string">&quot;, name=&#x27;&quot;</span> + name + <span class="string">&#x27;\&#x27;&#x27;</span> +</span><br><span class="line">                <span class="string">&quot;, age=&quot;</span> + age +</span><br><span class="line">                <span class="string">&quot;, register=&quot;</span> + register +</span><br><span class="line">                <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="储藏类"><a href="#储藏类" class="headerlink" title="储藏类"></a>储藏类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.repository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.trey.entity.Student;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentRepository</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> List&lt;Student&gt; <span class="title function_">findAll</span><span class="params">()</span>&#123;</span><br><span class="line">        ArrayList&lt;Student&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">PreparedStatement</span> <span class="variable">preparedStatement</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">ResultSet</span> <span class="variable">resultSet</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>);</span><br><span class="line">            <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql://1.1.1.3:3306/bookdb?useUnicode=true&amp;characterEncoding=UTF-8&quot;</span>;</span><br><span class="line">            <span class="type">String</span> <span class="variable">user</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">            <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;Passwd123&quot;</span>;</span><br><span class="line">            connection = DriverManager.getConnection(url,user,password);</span><br><span class="line">            <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from user;&quot;</span>;</span><br><span class="line">            preparedStatement = connection.prepareStatement(sql);</span><br><span class="line">            resultSet = preparedStatement.executeQuery();</span><br><span class="line">            <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">            <span class="keyword">while</span> (resultSet.next()) &#123;</span><br><span class="line">                <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> resultSet.getInt(<span class="number">1</span>);</span><br><span class="line">                <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> resultSet.getString(<span class="number">2</span>);</span><br><span class="line">                <span class="type">double</span> <span class="variable">age</span> <span class="operator">=</span> resultSet.getDouble(<span class="number">3</span>);</span><br><span class="line">                <span class="type">Date</span> <span class="variable">date</span> <span class="operator">=</span> resultSet.getDate(<span class="number">4</span>);</span><br><span class="line">                student = <span class="keyword">new</span> <span class="title class_">Student</span>(id, name, age, date);</span><br><span class="line">                list.add(student);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                connection.close();</span><br><span class="line">                preparedStatement.close();</span><br><span class="line">                resultSet.close();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> list;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">StudentRepository</span> <span class="variable">studentRepository</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StudentRepository</span>();</span><br><span class="line">        System.out.println(studentRepository.findAll());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="控制类"><a href="#控制类" class="headerlink" title="控制类"></a>控制类</h4><p>jsp接收数据，还需要两个包： jstl.jar 和standard.jar </p>
<p>(我下载好了，放到了 D:\install\apache-tomcat-8.5.78-windows-x64\jakarta-taglibs-standard-1.1.2 ）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.servlet;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.trey.entity.Student;</span><br><span class="line"><span class="keyword">import</span> com.trey.repository.StudentRepository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletException;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.annotation.WebServlet;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServlet;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="meta">@WebServlet(&quot;/student&quot;)</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentServlet</span> <span class="keyword">extends</span> <span class="title class_">HttpServlet</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">        <span class="comment">//返回试图+数据</span></span><br><span class="line">        <span class="type">StudentRepository</span> <span class="variable">studentRepository</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StudentRepository</span>();</span><br><span class="line">        List&lt;Student&gt; list = studentRepository.findAll();</span><br><span class="line">        System.out.println(list);</span><br><span class="line">        req.setAttribute(<span class="string">&quot;list&quot;</span>,list);                           <span class="comment">//数据</span></span><br><span class="line">        req.getRequestDispatcher(<span class="string">&quot;index.jsp&quot;</span>).forward(req,resp); <span class="comment">//视图</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">        <span class="built_in">super</span>.doPost(req, resp);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="试图index-jsp"><a href="#试图index-jsp" class="headerlink" title="试图index.jsp"></a>试图index.jsp</h4><figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line">&lt;%--</span><br><span class="line">  Created by IntelliJ IDEA.</span><br><span class="line">  User: Lenovo</span><br><span class="line">  Date: <span class="number">2022</span>/<span class="number">5</span>/<span class="number">22</span></span><br><span class="line">  Time: <span class="number">17</span>:<span class="number">23</span></span><br><span class="line">  To change <span class="built_in">this</span> template use File | Settings | File Templates.</span><br><span class="line">--%&gt;</span><br><span class="line">&lt;%@ page contentType=<span class="string">&quot;text/html;charset=UTF-8&quot;</span> language=<span class="string">&quot;java&quot;</span> %&gt;</span><br><span class="line">&lt;%@ taglib prefix=<span class="string">&quot;c&quot;</span> uri=<span class="string">&quot;http://java.sun.com/jsp/jstl/core&quot;</span> %&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;title&gt;$Title$&lt;/title&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">  &lt;h1&gt;学生管理系统&lt;/h1&gt;</span><br><span class="line">  &lt;table&gt;</span><br><span class="line">    &lt;th&gt;编号&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;姓名&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;成绩&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;注册日期&lt;/th&gt;</span><br><span class="line">    &lt;c:forEach items=<span class="string">&quot;$&#123;list&#125;&quot;</span> <span class="keyword">var</span>=<span class="string">&quot;student&quot;</span>&gt;</span><br><span class="line">      &lt;tr&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.id&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.name&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.age&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.register&#125;&lt;/td&gt;</span><br><span class="line">      &lt;/tr&gt;</span><br><span class="line">    &lt;/c:forEach&gt;</span><br><span class="line">  &lt;/table&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="查询功能完成"><a href="#查询功能完成" class="headerlink" title="查询功能完成"></a>查询功能完成</h4><p>启动项目</p>
<p>打开网页 ： <a target="_blank" rel="noopener" href="http://1.1.1.1:8080/student">http://1.1.1.1:8080/student</a></p>
<p>学生管理系统查询结果</p>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>成绩</th>
<th>注册日期</th>
</tr>
</thead>
<tbody><tr>
<td>5</td>
<td>张三</td>
<td>36.0</td>
<td>2022-05-22</td>
</tr>
<tr>
<td>6</td>
<td>李四</td>
<td>36.0</td>
<td>2022-05-22</td>
</tr>
<tr>
<td>7</td>
<td>王五</td>
<td>36.0</td>
<td>2022-05-22</td>
</tr>
</tbody></table>
<h2 id="java-增加-mysql"><a href="#java-增加-mysql" class="headerlink" title="java 增加 mysql"></a>java 增加 mysql</h2><h4 id="新建add-jsp"><a href="#新建add-jsp" class="headerlink" title="新建add.jsp"></a>新建add.jsp</h4><p>​	在web下面新建</p>
<figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;%@ page contentType=<span class="string">&quot;text/html;charset=UTF-8&quot;</span> language=<span class="string">&quot;java&quot;</span> %&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">    &lt;title&gt;添加记录&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;form action=<span class="string">&quot;/student&quot;</span> method=<span class="string">&quot;post&quot;</span>&gt;</span><br><span class="line">        姓名：&lt;input type=<span class="string">&quot;text&quot;</span> name=<span class="string">&quot;name&quot;</span>/&gt;&lt;br/&gt;</span><br><span class="line">        成绩：&lt;input type=<span class="string">&quot;text&quot;</span> name=<span class="string">&quot;age&quot;</span>/&gt;&lt;br/&gt;</span><br><span class="line">        &lt;input type=<span class="string">&quot;submit&quot;</span> value=<span class="string">&quot;提交&quot;</span>&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<h4 id="实现储藏类Repository的add方法"><a href="#实现储藏类Repository的add方法" class="headerlink" title="实现储藏类Repository的add方法"></a>实现储藏类Repository的add方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(String name,Double age)</span>&#123;</span><br><span class="line">        <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">PreparedStatement</span> <span class="variable">preparedStatement</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>);</span><br><span class="line">            <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql://1.1.1.3:3306/bookdb?useUnicode=true&amp;characterEncoding=UTF-8&quot;</span>;</span><br><span class="line">            <span class="type">String</span> <span class="variable">user</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">            <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;Passwd123&quot;</span>;</span><br><span class="line">            connection = DriverManager.getConnection(url,user,password);</span><br><span class="line">            <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;insert into user(name,age,register) values (?,?,?)&quot;</span>;</span><br><span class="line">            preparedStatement = connection.prepareStatement(sql);</span><br><span class="line">            preparedStatement.setString(<span class="number">1</span>,name);</span><br><span class="line">            preparedStatement.setDouble(<span class="number">2</span>,age);</span><br><span class="line">            preparedStatement.setDate(<span class="number">3</span>,<span class="keyword">new</span> <span class="title class_">Date</span>(System.currentTimeMillis()));</span><br><span class="line">            preparedStatement.executeUpdate();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                connection.close();</span><br><span class="line">                preparedStatement.close();</span><br><span class="line">            &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h4 id="实现Servlet的doPost方法"><a href="#实现Servlet的doPost方法" class="headerlink" title="实现Servlet的doPost方法"></a>实现Servlet的doPost方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">    <span class="comment">//处理汉字乱码方法1，此方法中把req转化成UTF-8编码</span></span><br><span class="line">    req.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">    <span class="comment">//处理汉字乱码方法2，过滤器</span></span><br><span class="line">    <span class="comment">//CharacterFilter.java</span></span><br><span class="line">    <span class="comment">//读取表单数据</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> req.getParameter(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">    <span class="type">String</span> <span class="variable">ageStr</span> <span class="operator">=</span> req.getParameter(<span class="string">&quot;age&quot;</span>);</span><br><span class="line">    <span class="type">Double</span> <span class="variable">age</span> <span class="operator">=</span> Double.parseDouble(ageStr);</span><br><span class="line">    <span class="comment">//执行insert</span></span><br><span class="line">    studentRepository.add(name,age);</span><br><span class="line">    <span class="comment">//返回重定向到查询页面</span></span><br><span class="line">    resp.sendRedirect(<span class="string">&quot;/student&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="过滤器"><a href="#过滤器" class="headerlink" title="过滤器"></a>过滤器</h2><p>注明：选中类名ctrl+i实现接口 。  如果什么都不写，就统统拦截</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.filter;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.*;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.annotation.WebFilter;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">//拦截</span></span><br><span class="line"><span class="meta">@WebFilter(&quot;/student&quot;)</span></span><br><span class="line"><span class="comment">//实现Filter接口</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">CharacterFilter</span> <span class="keyword">implements</span> <span class="title class_">Filter</span> &#123;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">init</span><span class="params">(FilterConfig filterConfig)</span> <span class="keyword">throws</span> ServletException &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">doFilter</span><span class="params">(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)</span> <span class="keyword">throws</span> IOException, ServletException &#123;</span><br><span class="line">        servletRequest.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">        filterChain.doFilter(servletRequest,servletResponse);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">destroy</span><span class="params">()</span> &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="优化-JDBCTools-提取封装"><a href="#优化-JDBCTools-提取封装" class="headerlink" title="优化 JDBCTools 提取封装"></a>优化 JDBCTools 提取封装</h2><h4 id="新建包类-JDBCTools"><a href="#新建包类-JDBCTools" class="headerlink" title="新建包类 JDBCTools"></a>新建包类 JDBCTools</h4><p>包 ： com.trey.tuil</p>
<p>类 ： JDBCTools</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDBCTools</span> &#123;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> Connection connection;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:mysql://1.1.1.3:3306/bookdb?useUnicode=true&amp;characterEncoding=UTF-8&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">user</span> <span class="operator">=</span> <span class="string">&quot;root&quot;</span>;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;Passwd123&quot;</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            Class.forName(<span class="string">&quot;com.mysql.jdbc.Driver&quot;</span>);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> Connection <span class="title function_">getConnection</span><span class="params">()</span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            connection = DriverManager.getConnection(url,user,password);</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> connection;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">release</span><span class="params">(Connection connection, Statement statement, ResultSet resultSet)</span>&#123;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (connection != <span class="literal">null</span>) &#123;</span><br><span class="line">                connection.close();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (statement != <span class="literal">null</span>) &#123;</span><br><span class="line">                statement.close();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (resultSet != <span class="literal">null</span>) &#123;</span><br><span class="line">                resultSet.close();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="修改Repository储藏类"><a href="#修改Repository储藏类" class="headerlink" title="修改Repository储藏类"></a>修改Repository储藏类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.repository;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> com.trey.entity.Student;</span><br><span class="line"><span class="keyword">import</span> com.trey.util.JDBCTools;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.Currency;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">StudentRepository</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> List&lt;Student&gt; <span class="title function_">findAll</span><span class="params">()</span>&#123;</span><br><span class="line">        ArrayList&lt;Student&gt; list = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">        <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">PreparedStatement</span> <span class="variable">preparedStatement</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">ResultSet</span> <span class="variable">resultSet</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            connection = JDBCTools.getConnection();</span><br><span class="line">            <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;select * from user;&quot;</span>;</span><br><span class="line">            preparedStatement = connection.prepareStatement(sql);</span><br><span class="line">            resultSet = preparedStatement.executeQuery();</span><br><span class="line">            <span class="type">Student</span> <span class="variable">student</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">            <span class="keyword">while</span> (resultSet.next()) &#123;</span><br><span class="line">                <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> resultSet.getInt(<span class="number">1</span>);</span><br><span class="line">                <span class="type">String</span> <span class="variable">name</span> <span class="operator">=</span> resultSet.getString(<span class="number">2</span>);</span><br><span class="line">                <span class="type">double</span> <span class="variable">age</span> <span class="operator">=</span> resultSet.getDouble(<span class="number">3</span>);</span><br><span class="line">                <span class="type">Date</span> <span class="variable">date</span> <span class="operator">=</span> resultSet.getDate(<span class="number">4</span>);</span><br><span class="line">                student = <span class="keyword">new</span> <span class="title class_">Student</span>(id, name, age, date);</span><br><span class="line">                list.add(student);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;  <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            JDBCTools.release(connection, preparedStatement, resultSet);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> list;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">StudentRepository</span> <span class="variable">studentRepository</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StudentRepository</span>();</span><br><span class="line">        System.out.println(studentRepository.findAll());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">add</span><span class="params">(String name,Double age)</span>&#123;</span><br><span class="line">        <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="type">PreparedStatement</span> <span class="variable">preparedStatement</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">        <span class="keyword">try</span> &#123;</span><br><span class="line">            connection = JDBCTools.getConnection();</span><br><span class="line">            <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;insert into user(name,age,register) values (?,?,?)&quot;</span>;</span><br><span class="line">            preparedStatement = connection.prepareStatement(sql);</span><br><span class="line">            preparedStatement.setString(<span class="number">1</span>,name);</span><br><span class="line">            preparedStatement.setDouble(<span class="number">2</span>,age);</span><br><span class="line">            preparedStatement.setDate(<span class="number">3</span>,<span class="keyword">new</span> <span class="title class_">Date</span>(System.currentTimeMillis()));</span><br><span class="line">            preparedStatement.executeUpdate();</span><br><span class="line">        &#125;  <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">            e.printStackTrace();</span><br><span class="line">        &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">            JDBCTools.release(connection,preparedStatement,<span class="literal">null</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="java-删除-mysql"><a href="#java-删除-mysql" class="headerlink" title="java 删除 mysql"></a>java 删除 mysql</h2><h4 id="修改index-jsp-提供页面操作入口"><a href="#修改index-jsp-提供页面操作入口" class="headerlink" title="修改index.jsp (提供页面操作入口)"></a>修改index.jsp (提供页面操作入口)</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">&lt;table&gt;</span><br><span class="line">    &lt;th&gt;编号&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;姓名&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;成绩&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;注册日期&lt;/th&gt;</span><br><span class="line">    &lt;th&gt;操作&lt;/th&gt;</span><br><span class="line">    &lt;c:forEach items=&quot;$&#123;list&#125;&quot; var=&quot;student&quot;&gt;</span><br><span class="line">      &lt;tr&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.id&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.name&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.age&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;$&#123;student.register&#125;&lt;/td&gt;</span><br><span class="line">        &lt;td&gt;</span><br><span class="line">          &lt;a href=&quot;/student?method=delete&amp;id=$&#123;student.id&#125;&quot;&gt;删除&lt;/a&gt;</span><br><span class="line">          &lt;a&gt;修改&lt;/a&gt;</span><br><span class="line">        &lt;/td&gt;</span><br><span class="line">      &lt;/tr&gt;</span><br><span class="line">    &lt;/c:forEach&gt;</span><br><span class="line">  &lt;/table&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="修改Servlet控制区分get请求"><a href="#修改Servlet控制区分get请求" class="headerlink" title="修改Servlet控制区分get请求"></a>修改Servlet控制区分get请求</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line">   <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doGet</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">       <span class="type">String</span> <span class="variable">method</span> <span class="operator">=</span> req.getParameter(<span class="string">&quot;method&quot;</span>);</span><br><span class="line">       <span class="keyword">if</span> (method == <span class="literal">null</span>) &#123;</span><br><span class="line">           method = <span class="string">&quot;findAll&quot;</span>;</span><br><span class="line">       &#125;</span><br><span class="line">       <span class="keyword">switch</span> (method) &#123;</span><br><span class="line">           <span class="keyword">case</span> <span class="string">&quot;findAll&quot;</span>:</span><br><span class="line">               <span class="comment">//返回试图+数据</span></span><br><span class="line">               List&lt;Student&gt; list = studentRepository.findAll();</span><br><span class="line">               req.setAttribute(<span class="string">&quot;list&quot;</span>,list);                           <span class="comment">//数据</span></span><br><span class="line">               req.getRequestDispatcher(<span class="string">&quot;index.jsp&quot;</span>).forward(req,resp); <span class="comment">//视图</span></span><br><span class="line">               <span class="keyword">break</span>;</span><br><span class="line">           <span class="keyword">case</span> <span class="string">&quot;delete&quot;</span>:</span><br><span class="line">               <span class="type">String</span> <span class="variable">idStr</span> <span class="operator">=</span> req.getParameter(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">               <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> Integer.parseInt(idStr);</span><br><span class="line">               studentRepository.deleteById(id);</span><br><span class="line">               resp.sendRedirect(<span class="string">&quot;/student&quot;</span>);</span><br><span class="line">               <span class="keyword">break</span>;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="实现储藏类delete方法"><a href="#实现储藏类delete方法" class="headerlink" title="实现储藏类delete方法"></a>实现储藏类delete方法</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">deleteById</span><span class="params">(Integer id)</span>&#123;</span><br><span class="line">     <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">     <span class="type">PreparedStatement</span> <span class="variable">preparedStatement</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">     connection = JDBCTools.getConnection();</span><br><span class="line">     <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;delete from user where id = ?&quot;</span>;</span><br><span class="line">     <span class="keyword">try</span> &#123;</span><br><span class="line">         preparedStatement = connection.prepareStatement(sql);</span><br><span class="line">         preparedStatement.setInt(<span class="number">1</span>,id);</span><br><span class="line">         preparedStatement.executeUpdate();</span><br><span class="line">     &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">         e.printStackTrace();</span><br><span class="line">     &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">         JDBCTools.release(connection, preparedStatement, <span class="literal">null</span>);</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="java-修改-mysql"><a href="#java-修改-mysql" class="headerlink" title="java 修改 mysql"></a>java 修改 mysql</h2><h4 id="修改index-jsp-提供页面操作入口-取id"><a href="#修改index-jsp-提供页面操作入口-取id" class="headerlink" title="修改index.jsp(提供页面操作入口) 取id"></a>修改index.jsp(提供页面操作入口) 取id</h4> <figure class="highlight jsp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;/head&gt;</span><br><span class="line">&lt;body&gt;</span><br><span class="line">&lt;h1&gt;学生管理系统&lt;/h1&gt;</span><br><span class="line">&lt;table&gt;</span><br><span class="line">  &lt;th&gt;编号&lt;/th&gt;</span><br><span class="line">  &lt;th&gt;姓名&lt;/th&gt;</span><br><span class="line">  &lt;th&gt;成绩&lt;/th&gt;</span><br><span class="line">  &lt;th&gt;注册日期&lt;/th&gt;</span><br><span class="line">  &lt;th&gt;操作&lt;/th&gt;</span><br><span class="line">  &lt;c:forEach items=<span class="string">&quot;$&#123;list&#125;&quot;</span> <span class="keyword">var</span>=<span class="string">&quot;student&quot;</span>&gt;</span><br><span class="line">    &lt;tr&gt;</span><br><span class="line">      &lt;td&gt;$&#123;student.id&#125;&lt;/td&gt;</span><br><span class="line">      &lt;td&gt;$&#123;student.name&#125;&lt;/td&gt;</span><br><span class="line">      &lt;td&gt;$&#123;student.age&#125;&lt;/td&gt;</span><br><span class="line">      &lt;td&gt;$&#123;student.register&#125;&lt;/td&gt;</span><br><span class="line">      &lt;td&gt;</span><br><span class="line">        &lt;a href=<span class="string">&quot;/student?method=deleteById&amp;id=$&#123;student.id&#125;&quot;</span>&gt;删除&lt;/a&gt;</span><br><span class="line">        &lt;a href=<span class="string">&quot;/student?method=findById&amp;id=$&#123;student.id&#125;&quot;</span>&gt;修改&lt;/a&gt;</span><br><span class="line">      &lt;/td&gt;</span><br><span class="line">    &lt;/tr&gt;</span><br><span class="line">  &lt;/c:forEach&gt;</span><br><span class="line">&lt;/table&gt;</span><br><span class="line">  &lt;a href=<span class="string">&quot;add.jsp&quot;</span>&gt;添加&lt;/a&gt;&lt;br/&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="根据id取出Student对象"><a href="#根据id取出Student对象" class="headerlink" title="根据id取出Student对象"></a>根据id取出Student对象</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">@Override</span><br><span class="line">   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException &#123;</span><br><span class="line">       String method = req.getParameter(&quot;method&quot;);</span><br><span class="line">       if (method == null) &#123;</span><br><span class="line">           method = &quot;findAll&quot;;</span><br><span class="line">       &#125;</span><br><span class="line">       switch (method) &#123;</span><br><span class="line">           case &quot;findAll&quot;:</span><br><span class="line">               //返回试图+数据</span><br><span class="line">               List&lt;Student&gt; list = studentRepository.findAll();</span><br><span class="line">               req.setAttribute(&quot;list&quot;,list);                           //数据</span><br><span class="line">               req.getRequestDispatcher(&quot;selectAll.jsp&quot;).forward(req,resp); //视图</span><br><span class="line">               break;</span><br><span class="line">           case &quot;deleteById&quot;:</span><br><span class="line">               String idStr = req.getParameter(&quot;id&quot;);</span><br><span class="line">               int id = Integer.parseInt(idStr);</span><br><span class="line">               studentRepository.deleteById(id);</span><br><span class="line">               resp.sendRedirect(&quot;/student&quot;);</span><br><span class="line">               break;</span><br><span class="line">           case &quot;findById&quot;:</span><br><span class="line">               idStr = req.getParameter(&quot;id&quot;);</span><br><span class="line">               id = Integer.parseInt(idStr);</span><br><span class="line">               req.setAttribute(&quot;student&quot;,studentRepository.findById(id));</span><br><span class="line">               req.getRequestDispatcher(&quot;update.jsp&quot;).forward(req,resp);</span><br><span class="line">               break;</span><br><span class="line">       &#125;</span><br><span class="line">   &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="根据Student对象填写update-jsp页面"><a href="#根据Student对象填写update-jsp页面" class="headerlink" title="根据Student对象填写update.jsp页面"></a>根据Student对象填写update.jsp页面</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;body&gt;</span><br><span class="line">    &lt;h1&gt;修改记录&lt;/h1&gt;</span><br><span class="line">    &lt;form action=&quot;/student&quot; method=&quot;post&quot;&gt;</span><br><span class="line">        编号：&lt;input type=&quot;text&quot; name=&quot;id&quot; value=&quot;$&#123;student.id&#125;&quot; readonly/&gt;&lt;br/&gt;</span><br><span class="line">        姓名：&lt;input type=&quot;text&quot; name=&quot;name&quot; value=&quot;$&#123;student.name&#125;&quot;/&gt;&lt;br/&gt;</span><br><span class="line">        成绩：&lt;input type=&quot;text&quot; name=&quot;age&quot; value=&quot;$&#123;student.age&#125;&quot;/&gt;&lt;br/&gt;</span><br><span class="line">        成绩：&lt;input type=&quot;text&quot; name=&quot;age&quot; value=&quot;$&#123;student.register &#125;&quot; readonly/&gt;&lt;br/&gt;</span><br><span class="line">        &lt;input type=&quot;hidden&quot; name=&quot;method&quot; value=&quot;update&quot;/&gt;</span><br><span class="line">        &lt;input type=&quot;submit&quot; value=&quot;修改&quot;&gt;</span><br><span class="line">    &lt;/form&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="修改update-jsp里面的值"><a href="#修改update-jsp里面的值" class="headerlink" title="修改update.jsp里面的值"></a>修改update.jsp里面的值</h4><p>​		网页操作，修改</p>
<h4 id="将新值传入update-sql语句，并执行，并返回新全表数据页面"><a href="#将新值传入update-sql语句，并执行，并返回新全表数据页面" class="headerlink" title="将新值传入update.sql语句，并执行，并返回新全表数据页面"></a>将新值传入update.sql语句，并执行，并返回新全表数据页面</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Override</span></span><br><span class="line"> <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doPost</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">     <span class="type">String</span> <span class="variable">method</span> <span class="operator">=</span> req.getParameter(<span class="string">&quot;method&quot;</span>);</span><br><span class="line">     <span class="keyword">switch</span> (method) &#123;</span><br><span class="line">         <span class="keyword">case</span> <span class="string">&quot;update&quot;</span>:</span><br><span class="line">             <span class="type">String</span> <span class="variable">idStr</span> <span class="operator">=</span> req.getParameter(<span class="string">&quot;id&quot;</span>);</span><br><span class="line">             <span class="type">int</span> <span class="variable">id</span> <span class="operator">=</span> Integer.parseInt(idStr);</span><br><span class="line">             name = req.getParameter(<span class="string">&quot;name&quot;</span>);</span><br><span class="line">             ageStr = req.getParameter(<span class="string">&quot;age&quot;</span>);</span><br><span class="line">             age = Double.parseDouble(ageStr);</span><br><span class="line">             studentRepository.update(id,name,age);</span><br><span class="line">             <span class="comment">//返回重定向到查询页面</span></span><br><span class="line">             resp.sendRedirect(<span class="string">&quot;/student&quot;</span>);</span><br><span class="line">             <span class="keyword">break</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="执行update-sql语句"><a href="#执行update-sql语句" class="headerlink" title="执行update.sql语句"></a>执行update.sql语句</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">void</span>  <span class="title function_">update</span><span class="params">(Integer id, String name, Double age)</span>&#123;</span><br><span class="line">      <span class="type">Connection</span> <span class="variable">connection</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">      <span class="type">PreparedStatement</span> <span class="variable">preparedStatement</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line">      <span class="keyword">try</span> &#123;</span><br><span class="line">          connection = JDBCTools.getConnection();</span><br><span class="line">          <span class="type">String</span> <span class="variable">sql</span> <span class="operator">=</span> <span class="string">&quot;update user set name = ? , age = ? , register = ? where id = ?&quot;</span>;</span><br><span class="line">          preparedStatement = connection.prepareStatement(sql);</span><br><span class="line">          preparedStatement.setString(<span class="number">1</span>,name);</span><br><span class="line">          preparedStatement.setDouble(<span class="number">2</span>,age);</span><br><span class="line">          preparedStatement.setDate(<span class="number">3</span>,<span class="keyword">new</span> <span class="title class_">Date</span>(System.currentTimeMillis()));</span><br><span class="line">          preparedStatement.setInt(<span class="number">4</span>,id);</span><br><span class="line">          preparedStatement.executeUpdate();</span><br><span class="line">      &#125;  <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">          e.printStackTrace();</span><br><span class="line">      &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">          JDBCTools.release(connection,preparedStatement,<span class="literal">null</span>);</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="后续汇总"><a href="#后续汇总" class="headerlink" title="后续汇总"></a>后续汇总</h2><p><img src="C:\Users\Lenovo\AppData\Roaming\Typora\typora-user-images\1653302798049.png" alt="1653302798049"></p>
<h1 id="informix报错情况"><a href="#informix报错情况" class="headerlink" title="informix报错情况"></a>informix报错情况</h1><h2 id="数据库没有启动"><a href="#数据库没有启动" class="headerlink" title="数据库没有启动"></a>数据库没有启动</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">java.sql.SQLException: com.gbasedbt.asf.IfxASFException: Attempt to connect to database server (g31) failed.</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqliConnect.&lt;init&gt;(IfxSqliConnect.java:1611)</span><br><span class="line">	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</span><br><span class="line">	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)</span><br><span class="line">	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)</span><br><span class="line">	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="字符集不匹配"><a href="#字符集不匹配" class="headerlink" title="字符集不匹配"></a>字符集不匹配</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">java.sql.SQLException: Unable to load locale categories.</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqliConnect.&lt;init&gt;(IfxSqliConnect.java:1436)</span><br><span class="line">	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</span><br><span class="line">	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)</span><br><span class="line">	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)</span><br><span class="line">	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="表不存在"><a href="#表不存在" class="headerlink" title="表不存在"></a>表不存在</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">java.sql.SQLException: com.gbasedbt.asf.IfxASFException: Attempt to connect to database server (g31) failed.</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqliConnect.&lt;init&gt;(IfxSqliConnect.java:1611)</span><br><span class="line">	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)</span><br><span class="line">	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)</span><br><span class="line">	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)</span><br><span class="line">	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="insert语句中出现非法字符"><a href="#insert语句中出现非法字符" class="headerlink" title="insert语句中出现非法字符"></a>insert语句中出现非法字符</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">环境 dbvis DB字符集gbk</span></span><br><span class="line">jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;NEWCODESET=utf8,GB18030-2000,5488;IFX_LOCK_MODE_WAIT=30</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">语句</span></span><br><span class="line">insert into user(name,age) values(&#x27;汪&#x27;,1);    --这一句报错</span><br><span class="line">insert into user(name,age) values(&#x27;伟&#x27;,1);    --这一句报错</span><br><span class="line">insert into user(name,age) values(&#x27;汪伟&#x27;,1);  --这一句不报错</span><br><span class="line">这里的报错和报错说明，utf8-&gt;gb18030的字符转换有问题。</span><br><span class="line">如果此时关闭过滤器，那么java中insert就不会报错，插入的汪是：c3 a6 c2 b1 c2 aa</span><br><span class="line">汪 utf8： e6 b1 aa 对应字符 f 1 *</span><br><span class="line">汪 gbk ： cd f4    对应字符 M t</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">报错</span></span><br><span class="line">[Code: -202, SQL State: IX000]  An illegal character has been found in the statement.</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">处理：把NEWCODESET设置为jbk即可，但是这f样如果之前入口的数据库是utf8的就存在不一致编码数据</span></span><br><span class="line">jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;NEWCODESET=GB18030,GB18030-2000,5488;IFX_LOCK_MODE_WAIT=30</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Java里面也会遇到insert语句中出现非法字符"><a href="#Java里面也会遇到insert语句中出现非法字符" class="headerlink" title="Java里面也会遇到insert语句中出现非法字符"></a>Java里面也会遇到insert语句中出现非法字符</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">java.sql.SQLException: An illegal character has been found in the statement.</span><br><span class="line">	at com.gbasedbt.util.IfxErrMsg.getSQLException(IfxErrMsg.java:408)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqli.a(IfxSqli.java:3572)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqli.D(IfxSqli.java:3852)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2738)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="Java里面遇到了不能解码的数据select语句"><a href="#Java里面遇到了不能解码的数据select语句" class="headerlink" title="Java里面遇到了不能解码的数据select语句"></a>Java里面遇到了不能解码的数据select语句</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">java.sql.SQLException: Encoding or code set not supported.</span><br><span class="line">	at com.gbasedbt.util.IfxErrMsg.getSQLException(IfxErrMsg.java:408)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxVarChar.fromIfx(IfxVarChar.java:195)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxRowColumn.a(IfxRowColumn.java:335)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="字符集不存在"><a href="#字符集不存在" class="headerlink" title="字符集不存在"></a>字符集不存在</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Error -23101 Unable to load locale categories</span><br><span class="line"></span><br><span class="line">当设置的 DB_LOCALE 和 CLIENT_LOCALE 的字符集对应的以下文件不存在时，出现该错误。</span><br><span class="line"></span><br><span class="line">    - $InformixDIR/gls/lc11/DB_LOCALE&#x27;s( 语言 _ 地区 )/(db 的 16 进制编码 ).lco</span><br><span class="line">    - $InformixDIR/gls/lc11/CLIENT_LOCALE&#x27;s( 语言 _ 地区 )/( db 的 16 进制编码 ).lco</span><br><span class="line">    - $InformixDIR/gls/lc11/CLIENT_LOCALE&#x27;s( 语言 _ 地区 )/( client 的 16 进制编码 ).lco</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="字符集不能转换"><a href="#字符集不能转换" class="headerlink" title="字符集不能转换"></a>字符集不能转换</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Error -23104 Error opening required code-set conversion object file</span><br><span class="line"></span><br><span class="line">当设置的 DB_LOCALE 和 CLIENT_LOCALE 的字符集对应的以下转换文件不存在时，会出现该错误。当然只有当 DB_LOCALE 和 CLIENT_LOCALE 的字符集不一致时才会需要转换，如果一致则不会出现 -23104 错误。</span><br><span class="line"></span><br><span class="line"> - $InformixDIR/gls/cv9/ccccdddd.cvo </span><br><span class="line"> - $InformixDIR/gls/cv9/ddddcccc.cvo</span><br><span class="line"></span><br><span class="line">其中：cccc 为 CLIENT_LOCALE 字符集编码对应的 16 进制值</span><br><span class="line"></span><br><span class="line">dddd 为 DB_LOCALE 字符集编码对应的 16 进制值</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="字符集DB-LOCALE不匹配"><a href="#字符集DB-LOCALE不匹配" class="headerlink" title="字符集DB_LOCALE不匹配"></a>字符集DB_LOCALE不匹配</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Error -23197 Database locale information mismatch</span><br><span class="line"></span><br><span class="line">当出现如下情况时，出现 -23197 错误。</span><br><span class="line"></span><br><span class="line">    定义的 DB_LOCALE 值与数据库的使用的值（数据库创建时使用的 DB_LOCALE 值）不一致；</span><br><span class="line">    通过 SET COLLATION 语句定义 DB_LOCALE 值与数据库的使用值不一致 ;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="字符集存在非法字符"><a href="#字符集存在非法字符" class="headerlink" title="字符集存在非法字符"></a>字符集存在非法字符</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Error -201，-202 数据库提示语法错误</span><br><span class="line"></span><br><span class="line">Error -201，-202 数据库提示语法错误，不支持中文对象名，如中文表名、字段别名、视图名。该类错误提示原因是当前数据库的 DB_LOCALE 设置问题。</span><br><span class="line"></span><br><span class="line">如果数据库的 DB_LOCALE 设置为 zh_cn.GB18030-2000，则数据库就可以支持中文对象名。</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h1 id="Servlet-JSP-JDBC-把库换成GBasedb8s"><a href="#Servlet-JSP-JDBC-把库换成GBasedb8s" class="headerlink" title="Servlet + JSP + JDBC 把库换成GBasedb8s"></a>Servlet + JSP + JDBC 把库换成GBasedb8s</h1><h2 id="informix-创建user表"><a href="#informix-创建user表" class="headerlink" title="informix 创建user表"></a>informix 创建user表</h2><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">DROP</span> <span class="keyword">TABLE</span> IF <span class="keyword">EXISTS</span> <span class="keyword">user</span>;</span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> <span class="keyword">user</span>  (</span><br><span class="line">  id  serial <span class="keyword">not</span> <span class="keyword">null</span> ,</span><br><span class="line">  name <span class="type">varchar</span>(<span class="number">9</span>) <span class="keyword">not</span> <span class="keyword">null</span>,</span><br><span class="line">  age <span class="type">int</span>,</span><br><span class="line">  register datetime <span class="keyword">year</span> <span class="keyword">to</span> <span class="keyword">second</span>,</span><br><span class="line">  <span class="keyword">PRIMARY</span> KEY (id) </span><br><span class="line">) extent size <span class="number">64</span> next size <span class="number">128</span> </span><br><span class="line">;</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name,age,register) <span class="keyword">values</span>(<span class="string">&#x27;张三&#x27;</span>,<span class="number">36</span>,<span class="string">&#x27;2022-05-22 08:00:00&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name,age,register) <span class="keyword">values</span>(<span class="string">&#x27;李四&#x27;</span>,<span class="number">36</span>,<span class="string">&#x27;2022-05-22 08:00:00&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> <span class="keyword">user</span>(name,age,register) <span class="keyword">values</span>(<span class="string">&#x27;王五&#x27;</span>,<span class="number">36</span>,<span class="string">&#x27;2022-05-22 08:00:00&#x27;</span>);</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="新建JDBCToolsGBase类"><a href="#新建JDBCToolsGBase类" class="headerlink" title="新建JDBCToolsGBase类"></a>新建JDBCToolsGBase类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.trey.util;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.sql.*;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">JDBCToolsGBase</span> &#123;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> Connection connection;</span><br><span class="line">        <span class="comment">//DB是utf8库编码</span></span><br><span class="line">        <span class="comment">//DB_LOCALE=zh_CN.utf8</span></span><br><span class="line">        <span class="comment">//dbs_dbsname  utf8</span></span><br><span class="line">        <span class="comment">//dbs_collate  zh_CN.57372</span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">urlutf8</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/utf8:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//DB是819库编码</span></span><br><span class="line">        <span class="comment">//DB_LOCALE=en_US.819</span></span><br><span class="line">        <span class="comment">//dbs_dbsname  sysmaster</span></span><br><span class="line">        <span class="comment">//dbs_collate  en_US.819</span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">url819utf8</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/sysmaster:GBASEDBTSERVER=g31;DB_LOCALE=en_US.819;NEWCODESET=utf8,8859-1,819;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">url819gbk</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/sysmaster:GBASEDBTSERVER=g31;DB_LOCALE=en_US.819;NEWCODESET=GB18030-2000,8859-1,819;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line">        <span class="comment">//DB是gbk库编码</span></span><br><span class="line">        <span class="comment">//DB_LOCALE=zh_CN.GB18030-2000</span></span><br><span class="line">        <span class="comment">//dbs_dbsname  gbk</span></span><br><span class="line">        <span class="comment">//dbs_collate  zh_CN.5488</span></span><br><span class="line">        <span class="comment">//这样可以正常处理utf8终端插入的汉字,再次证明解铃还须系铃人，NEWCODESET=jdk编码集.校验字符集.校验字符编号。</span></span><br><span class="line">        <span class="comment">// （注意这时如果关闭过滤器：会出现页面insert的数据是乱码的问题，应为这时页面以gbk的数据把数据存入了gbk的库，但是你读取的时候又把jbk的数据转成了utf8）</span></span><br><span class="line">        <span class="comment">// （后来发现如果关闭过滤器，插入如的数据，是什么编码，情况比上面我说的还要复杂，它并不是gbk，也不是utf8.他1个汉字就占6个字符。）</span></span><br><span class="line">        <span class="comment">//private static String urlgbk = &quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;NEWCODESET=utf8,GB18030-2000,5488;IFX_LOCK_MODE_WAIT=30&quot;;</span></span><br><span class="line">        <span class="comment">//而这样可以正常处理gbk终端插入的汉字,正确。</span></span><br><span class="line">        <span class="comment">//private static String urlgbk = &quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;NEWCODESET=GB18030,GB18030-2000,5488;IFX_LOCK_MODE_WAIT=30&quot;;</span></span><br><span class="line">        <span class="comment">//NEWCODESET可以省略</span></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">urlgbk</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line">        <span class="comment">//一句话： NEWCODESET的第一个字段是你的数据存储表中的编码，它等于你的终端的编码，或者说它等于你的jdk的编码。</span></span><br><span class="line">        <span class="comment">//两句话： 我们平常说的数据库的字符集，其实准确的说是数据库校验字符集，它是做校验的，它不是实际存储的表的字符集。</span></span><br><span class="line">        <span class="comment">//三句话： 推荐校验字符集和终端字符集保持统一字符集，那么就不需要NEWCODESET了。</span></span><br><span class="line">        <span class="comment">//四句话： 为了防止终端字符集把不同的数据编码存入表中，可以开启NEWCODESET，它保证了读写表的字符集。</span></span><br><span class="line">        <span class="comment">//五句话： 如果没有设置NEWCODESET那么，默认终端或jdk保证了表的字符集。</span></span><br><span class="line">        <span class="comment">//最后一句： 过滤器不能关，关了表里的编码很乱。要保证过滤器的字符集和校验字符集一致，这样就避免了NEWCODESET的校验，这样就不会遇到&#x27;汪&#x27;字不能插入的问题。（入库时的校验只是为了出库时的正常，并不实际转换。）</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">user</span> <span class="operator">=</span> <span class="string">&quot;gbasedbt&quot;</span>;</span><br><span class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">password</span> <span class="operator">=</span> <span class="string">&quot;GBase123&quot;</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">static</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                Class.forName(<span class="string">&quot;com.gbasedbt.jdbc.Driver&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (ClassNotFoundException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">static</span> Connection <span class="title function_">getConnection</span><span class="params">()</span>&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="comment">//connection = DriverManager.getConnection(urlutf8,user,password);</span></span><br><span class="line">                <span class="comment">//connection = DriverManager.getConnection(url819,user,password);</span></span><br><span class="line">                connection = DriverManager.getConnection(urlgbk,user,password);</span><br><span class="line">            &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> connection;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">release</span><span class="params">(Connection connection, Statement statement, ResultSet resultSet)</span>&#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="keyword">if</span> (connection != <span class="literal">null</span>) &#123;</span><br><span class="line">                    connection.close();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (statement != <span class="literal">null</span>) &#123;</span><br><span class="line">                    statement.close();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">if</span> (resultSet != <span class="literal">null</span>) &#123;</span><br><span class="line">                    resultSet.close();</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">catch</span> (SQLException e) &#123;</span><br><span class="line">                e.printStackTrace();</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="把储藏类连接数据可类换成此类即可"><a href="#把储藏类连接数据可类换成此类即可" class="headerlink" title="把储藏类连接数据可类换成此类即可"></a>把储藏类连接数据可类换成此类即可</h2><p>​		简单省略</p>
<h1 id="INFOMRIX-和-JDK的字符集设置"><a href="#INFOMRIX-和-JDK的字符集设置" class="headerlink" title="INFOMRIX 和 JDK的字符集设置"></a>INFOMRIX 和 JDK的字符集设置</h1><h2 id="创建utf8编码的数据库"><a href="#创建utf8编码的数据库" class="headerlink" title="创建utf8编码的数据库"></a>创建utf8编码的数据库</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">export DB_LOCALE=zh_CN.utf8</span><br><span class="line">export CLIENT_LOCALE=zh_CN.utf8</span><br><span class="line">echo &quot;create database gbk in datadbs01 with log;&quot; |dbaccess sysmaster </span><br><span class="line">echo &quot;select * from sysdbslocale;&quot; |dbaccess sysmaster </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="创建gbk编码的数据库"><a href="#创建gbk编码的数据库" class="headerlink" title="创建gbk编码的数据库"></a>创建gbk编码的数据库</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">export DB_LOCALE=zh_CN.GB18030-2000</span><br><span class="line">export CLIENT_LOCALE=zh_CN.GB18030-2000</span><br><span class="line">echo &quot;create database gbk in datadbs01 with log;&quot; |dbaccess sysmaster </span><br><span class="line">echo &quot;select * from sysdbslocale;&quot; |dbaccess sysmaster </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="我java项目用的是utf8编码"><a href="#我java项目用的是utf8编码" class="headerlink" title="我java项目用的是utf8编码"></a>我java项目用的是utf8编码</h2><p>​		见上文过滤器</p>
<h2 id="informix-GLS-编码和转换"><a href="#informix-GLS-编码和转换" class="headerlink" title="informix GLS 编码和转换"></a>informix GLS 编码和转换</h2><h3 id="编码对照表-注册registry"><a href="#编码对照表-注册registry" class="headerlink" title="编码对照表-注册registry"></a>编码对照表-注册registry</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">InformixDIR=/opt/gbase330</span><br><span class="line">cat $InformixDIR/gls/cm3/registry    #查看 GLS 字符名称、编码对照表。</span><br><span class="line">gbasedbt:g31[/opt/gbase330/gls]$cat $InformixDIR/gls/cm3/registry |grep 18030</span><br><span class="line">gbasedbt:g31[/opt/gbase330/gls]$cat $InformixDIR/gls/cm3/registry |grep -i utf8</span><br><span class="line">gbasedbt:g31[/opt/gbase330/gls]$cat $InformixDIR/gls/cm3/registry |grep -i e00d</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">结果</span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">字符集名称  	编码  	十六进制编码</span></span><br><span class="line">GB18030-2000    5488    # 0x1570 Simplified Chinese GB18030-2000</span><br><span class="line">utf8            57372   # 0xe01c 注意：utf8是72</span><br><span class="line">gb              57357   # 0xe00d 注意：  gb是57</span><br><span class="line">GB2312-80       57357   # 0xe00d</span><br><span class="line">cp936           57357   # 0xe00d Simplified Chinese Microsoft Windows </span><br><span class="line">Guobiao2312     57357   # 0xe00d</span><br><span class="line">gbasedbt:g31[/opt/gbase330/gls]$</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="一个字符集可以有多个名称"><a href="#一个字符集可以有多个名称" class="headerlink" title="一个字符集可以有多个名称"></a>一个字符集可以有多个名称</h3><p>​		 GLS 环境中不同字符集名称可能对应同一个字符集编码，但一个字符集只能有一个编码，也就是说字符集编码才是唯一的。 </p>
<h3 id="字符集存放在不同的目录"><a href="#字符集存放在不同的目录" class="headerlink" title="字符集存放在不同的目录"></a>字符集存放在不同的目录</h3><p>​		GLS 环境中按照语言和地区把所支持的字符集分成不同的目录。$InformixDIR&#x2F;gls&#x2F;lc11&#x2F; 语言 _ 地区 &#x2F;，如中文大陆地区的目录为：$InformixDIR&#x2F;gls&#x2F;lc11&#x2F;zh_cn&#x2F;，该目录下有如下两个文件：1570.lco e00d.lco ，说明我们在设置字符集时，我们可以使用 zh_cn.GB18030-2000 zh_cn.gb zh_cn.GB2312-80 三个不同的名称。这里（zh_cn.gb 与 zh_cn.GB2312-80 对应相同的字符集）。</p>
<h3 id="那些字符集直接可以转换"><a href="#那些字符集直接可以转换" class="headerlink" title="那些字符集直接可以转换"></a>那些字符集直接可以转换</h3><p>​		 GLS 环境中不同的字符集可以正确的进行转换，查看那些字符集可以正确转换的方法，查看目录 $InformixDIR&#x2F;gls&#x2F;cv9  目录下的是否存在指定字符集互相转换的文件。如该目录下有文件 e01ce00d.cvo 和 e00de01c.cvo 两个文件，表示 GLS  通过这两个转换文件支持 UTF-8 与 GB 之间的字符转换。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">InformixDIR=/opt/gbase330</span><br><span class="line">ls -l $InformixDIR/gls/cv9</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">查看utf8可以转换成那些字符集</span></span><br><span class="line">ls -l $InformixDIR/gls/cv9 |grep &#x27; e01c&#x27;</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">查看那些字符集可以转换成utf8</span></span><br><span class="line">ls -l $InformixDIR/gls/cv9 |grep &#x27;e01c\.&#x27;</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">查看utf8和GB18030-2000能否相互转换(注明：可以)</span></span><br><span class="line">ls -l $InformixDIR/gls/cv9 |grep &#x27;e01c&#x27;|grep &#x27;1570&#x27; </span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">查看utf8和gb能否相互转换(注明：可以)</span></span><br><span class="line">ls -l $InformixDIR/gls/cv9 |grep &#x27;e01c&#x27;|grep &#x27;e00d&#x27;  </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="DB-LOCALE-和-CLIENT-LOCALE"><a href="#DB-LOCALE-和-CLIENT-LOCALE" class="headerlink" title="DB_LOCALE 和 CLIENT_LOCALE"></a>DB_LOCALE 和 CLIENT_LOCALE</h3><p>Informix 通过 DB_LOCALE 和 CLIENT_LOCALE 来设置数据库的语言本地化支持设置。DB_LOCALE 和 CLIENT_LOCALE 的值由四部分组成 ( 第 4 部分为可选 )，字符集不区分大小写。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">   1          2            3          4 </span><br><span class="line">&lt; 语言 &gt;_&lt; 国家和地区 &gt;.&lt; 字符集名 / 字符集编码 &gt;[@modifier]</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>举例说明 :</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CLIENT_LOCALE=en_us.8859-1 </span><br><span class="line">CLIENT_LOCALE=en_us.819 </span><br><span class="line"># 以上两个为同一字符集：819 为 8859-1 的编码</span><br><span class="line">DB_LOCALE=zh_cn.gb</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="DB-LOCALE-环境变量用途"><a href="#DB-LOCALE-环境变量用途" class="headerlink" title="DB_LOCALE 环境变量用途"></a>DB_LOCALE 环境变量用途</h4><ol>
<li>在客户机应用程序和数据库服务器交换字符数据时，如果 DB_LOCALE 环境变量（在客户机计算机上）的值与 CLIENT_LOCALE 的值不同，客户机应用程序将执行代码集转换。 代码集转换防止这两种代码集不同时发生数据破坏。</li>
<li>在客户机应用程序请求连接时，它将包括 DB_LOCALE（如果已设置）的信息发送至数据库服务器。</li>
<li>在确定如何设置服务器处理语言环境的数据库信息时，数据库服务器使用 DB_LOCALE。</li>
<li>在客户机应用程序尝试打开数据库时，数据库服务器将客户机应用程序传递的 DB_LOCALE 环境变量的值与数据库中存储的数据库语言环境进行比较。</li>
<li>当数据库服务器存取与语言环境相关的数据类型的列时，数据库服务器使用 DB_LOCALE 指定的语言环境。</li>
<li>当数据库服务器创建新数据库时，它将检查数据库语言环境（DB_LOCALE），以确定如何在数据库的系统目录中存储字符信息。此信息包括诸如如何处理正则表达式、比较字符串以及确保代码集的正确使用的操作。</li>
</ol>
<h4 id="CLIENT-LOCALE-环境变量用途"><a href="#CLIENT-LOCALE-环境变量用途" class="headerlink" title="CLIENT_LOCALE 环境变量用途"></a>CLIENT_LOCALE 环境变量用途</h4><ol>
<li>在客户机应用程序和数据库服务器交换字符数据时，如果 CLIENT_LOCALE 环境变量的代码集与 DB_LOCALE（在客户机计算机上）的代码集不同，客户机应用程序将执行代码集转换。代码集转换防止这两种代码集不同时发生数据破坏。</li>
<li>在客户机应用程序请求连接时，它将包括 CLIENT_LOCALE 的信息发送至数据库服务器。</li>
<li>在确定如何设置服务器处理语言环境的客户机应用程序信息时，数据库服务器将使用 CLIENT_LOCALE。</li>
<li>在 Informix ESQL&#x2F;C 的预处理器处理源文件时，它接受以 CLIENT_LOCALE 的代码集编写的 C 源代码。 在  Informix ESQL&#x2F;C 客户机应用程序执行时，将检查 CLIENT_LOCALE  以获得客户机语言环境的名称，该语言环境将对操作系统文件名、文本文件的内容以及日期、时间和数字数据的格式产生影响。</li>
<li>在数据库实用程序创建文件时，文件名和文件内容位于 CLIENT_LOCALE 指定的代码集中。在查找特定于产品的消息文件时，客户机应用程序将检查与客户机语言环境关联的消息目录。</li>
</ol>
<h3 id="客户机应用程序代码集转换"><a href="#客户机应用程序代码集转换" class="headerlink" title="客户机应用程序代码集转换"></a>客户机应用程序代码集转换</h3><p>当以下两个条件都为真时，客户机应用程序自动在客户机和数据库代码集之间执行代码集转换：</p>
<ul>
<li>客户机和数据库语言环境的代码集不匹配。</li>
<li>客户机和数据库代码集之间的转换存在有效的目标代码集转换。</li>
</ul>
<p>客户机应用程序开始执行时，它会比较客户机和数据库语言环境的名称，以确定是否执行代码集转换。如果设置了 CLIENT_LOCALE 和  DB_LOCALE 环境变量，那么客户机应用程序使用这些语言环境名称来分别确定客户机和数据库的代码集。如果未设置  CLIENT_LOCALE（且未设置 DBNLS），那么客户机应用程序假定客户机语言环境为缺省语言环境。如果未设置 DB_LOCALE（且未设置 DBNLS），那么客户机应用程序假定数据库语言环境与客户机语言环境（CLIENT_LOCALE 设置的值）相同。</p>
<p>​		CLIENT_LOCALE( 未设置将采用默认 en_us.819)；</p>
<p>​		DB_LOCALE( 未设置则不发送，默认与CLIENT_LOCALE设置的值相同 )</p>
<h3 id="时间格式问题"><a href="#时间格式问题" class="headerlink" title="时间格式问题"></a>时间格式问题</h3><p>Informix 数据库的时间格式由数据库服务器端环境变量 GL_DATE GL_DATETIME 控制，默认的字符集下默认的时间格式为：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">GL_DATE=&quot;%m/%d/%iy&quot; </span><br><span class="line">DATETIME=&quot;%iY-%m-%d %H:%M:%S&quot;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>但是，当我们设置了 DB_LOCALE 为 zh_cn.gb 的情形下，而没有设置 GL_DATE,DATETIME，则时间格式会采用  CLIENT_LOCALE 的值，在 zh_cn.gb 情况下，会出现：“2009 年 10 月 2  日”的日期格式，如果我们之前系统采用默认的时间格式的情况下，就会出现时间格式不匹配的错误。如果我们仍然需要采用默认的时间格式，我们需要在数据库服务端修改时间格式环境变量即可：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">GL_DATE=&quot;%m/%d/%iy&quot; </span><br><span class="line">DATETIME=&quot;%iY-%m-%d %H:%M:%S&quot;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="换成unicode"><a href="#换成unicode" class="headerlink" title="换成unicode"></a>换成unicode</h3><p>数据库不能够脱离字符集独立存在，它一定属于某种字符集。</p>
<p>CSDK2.7 版本，IDS 默认情况下使用 Garbage In, Garbage Out 模式处理中文字符，若数据库服务器上的  DB_LOCALE 采用默认的 en_us.8859-1 字符集，能够正常支持中文字符。但是升级到 CSDK2.8 及以上版本时，不再支持  Garbage In, Garbage Out  模式，将出现乱码问题。iso-8859_1字符集的数据库只支持单字节。虽然可以往里面保存多字节的数据，但是数据库本身只是简单的认为这些数据只是单字节的数据。无论你存入的是双字节的中文，还是4字节的unicode数据，数据库本身只是把它当为单字节而已。</p>
<p>客户端通过jdbc连接上数据库，并提取数据后，数据按照数据库默认的编码方式被提取到客户端。客户端要将其转换成unicode。因为jdbc是基于java的，而java内部全部是unicode编码。那么在客户端进行转换的时候，就有问题了，到底按哪种格式转？</p>
<p>如果保存在数据库中的数据都是iso-8859_1中包含的字符，那么这样操作没错。但是如果是数据中有中文，那么这样操作就错了。</p>
<p>例如，汉字“中文”的ascii码为0xD6 0xD0 0xCE  0xC4,而常用的汉字codeset为cp936。如果按照cp936转unicode,则转换的结果是0xE4 0xB8 0xAD 0xE6  0x96 0x87;如果按照iso-8859_1转换则结果是0xC3 0x96 0xC3 0x90 0xC3 0x8E 0xC3 0x84.</p>
<p>你看，同样的数据，就是因为指定不同的原始字符集，导致转换成unicode的时候，结果截然不同。所以在用jdbc连接数据库的时候，有必要把这个转换关系给说清楚。</p>
<p>在informix jdbc中是通过newcodeset来说明问题的。</p>
<p>在这里，我们就要说清楚，数据库是按什么字符集存放数据的，取到客户端java时，这些数据<strong>需要按照什么样的字符集转换成unicode。</strong></p>
<p>再或者更改数据库的字符集（设置 DB_LOCALE&#x3D;zh_cn.GB18030-2000，重新创建数据库），然后按本文中描述的方法进行  DB_LOCALE 与 CLIENT_LOCALE 的设置方法进行处理。若在实际环境下重建数据库成本太高，可以考虑如下步骤进行解决 ODBC  支持中文的问题。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">数据库服务器端：</span><br><span class="line"> 1. 设置环境变量： IFMX_UNDOC_B168163=1 </span><br><span class="line"> 2. 将 en_us.8859-1 字符集文件拷贝到 zh_cn 目录下</span><br><span class="line">     cd $INFORMIXDIR/gls/lc11 </span><br><span class="line">     cp ./en_us/0333.lco ./zh_cn </span><br><span class="line"> 3. 重新启动 IDS </span><br><span class="line">客户端：</span><br><span class="line">设置语言环境：</span><br><span class="line">     l DB_LOCALE=zh_cn.GB18030-2000 </span><br><span class="line">     l CLIENT_LOCALE=zh_cn.GB18030-2000</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p> 对于 JDBC 我们可以通过 NEWCODESET 来解决该问题： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">newcodeset的格式如下：</span><br><span class="line">newcodeset=jdk codeset, ifx codeset,ifx codenum</span><br><span class="line"></span><br><span class="line">jdbc的URL链接信息如下：</span><br><span class="line"></span><br><span class="line">URLString = <span class="string">&quot;jdbc:Informix-sqli://9.125.66.130:6346/dbname:InformixSERVER=servername; NEWCODESET=GB18030-2000,8859-1,819; CLIENT_LOCALE=en_US.8859-1; DB_LOCALE=en_US.8859-1;&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="数据库编码对照表-和-jdbc-url"><a href="#数据库编码对照表-和-jdbc-url" class="headerlink" title="数据库编码对照表 和 jdbc-url"></a>数据库编码对照表 和 jdbc-url</h2><table>
<thead>
<tr>
<th>环境变量DB_LOCALE</th>
<th>DB名称</th>
<th>DB校对dbs_collate</th>
<th>NEWCODESET</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td>zh_CN.utf8</td>
<td>utf8</td>
<td>zh_CN.57372</td>
<td>可以空 utf8,utf8,57372<br/>强换 GB18030-2000,utf8,57372</td>
<td></td>
</tr>
<tr>
<td>en_US.819</td>
<td>sysmaster</td>
<td>en_US.819</td>
<td>不能空 utf8,8859-1,819 <br/>或  GB18030-2000,8859-1,819</td>
<td></td>
</tr>
<tr>
<td>zh_CN.GB18030-2000</td>
<td>gbk</td>
<td>zh_CN.5488</td>
<td>可以空GB18030-2000,GB18030-2000,5488 <br/>强转utf8,GB18030-2000,5488</td>
<td></td>
</tr>
</tbody></table>
<h3 id="强制转换对Java的要求"><a href="#强制转换对Java的要求" class="headerlink" title="强制转换对Java的要求"></a>强制转换对Java的要求</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">servletRequest.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line">filterChain.doFilter(servletRequest,servletResponse);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="utf8-8859-1-GB18030"><a href="#utf8-8859-1-GB18030" class="headerlink" title="utf8 + 8859-1 + GB18030"></a>utf8 + 8859-1 + GB18030</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">urlutf8</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/utf8:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">urlutf8gbk</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/utf8:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.utf8;CLIENT_LOCALE=zh_CN.utf8;NEWCODESET=GB18030-2000,utf8,57372;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">url819utf8</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/sysmaster:GBASEDBTSERVER=g31;DB_LOCALE=en_US.819;NEWCODESET=utf8,8859-1,819;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">url819gbk</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/sysmaster:GBASEDBTSERVER=g31;DB_LOCALE=en_US.819;NEWCODESET=GB18030-2000,8859-1,819;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">urlgbk</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="type">String</span> <span class="variable">urlgbkutf8</span> <span class="operator">=</span> <span class="string">&quot;jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;NEWCODESET=utf8,GB18030-2000,5488;IFX_LOCK_MODE_WAIT=30&quot;</span>;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="字符转换测试小结"><a href="#字符转换测试小结" class="headerlink" title="字符转换测试小结"></a>字符转换测试小结</h3><pre><code>    //一句话： NEWCODESET的第一个字段是JDK存取数据的编码，它等于你的终端的编码，或者说它等于你的jdk的编码。
    //两句话： 我们平常说的数据库的字符集，其实准确的说是数据库校验字符集，它是做校验的，它尽量是实际存储的表的字符集。
    //三句话： 推荐校验字符集和终端字符集保持统一字符集，那么就不需要NEWCODESET了。
    //四句话： 为了防止终端字符集把不同的数据编码存入表中，可以开启NEWCODESET，但要三个字段相同。
    //五句话： 如果没有设置NEWCODESET那么，默认终端或jdk字符集。
    //最后一句： 过滤器不能关，关了表里的编码很乱。要保证过滤器的字符集和校验字符集一致，这样就避免了NEWCODESET的校验，这样就不会遇到&#39;汪&#39;字不能插入的问题。（入库时的校验只是为了出库时的正常，值校验并不实际转换。）
</code></pre>
<h3 id="这种报错就是jdk的编码和jdbc里面设置的NEWCODESET的校验码不一致"><a href="#这种报错就是jdk的编码和jdbc里面设置的NEWCODESET的校验码不一致" class="headerlink" title="这种报错就是jdk的编码和jdbc里面设置的NEWCODESET的校验码不一致"></a>这种报错就是jdk的编码和jdbc里面设置的NEWCODESET的校验码不一致</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">java.sql.SQLException: An illegal character has been found in the statement.</span><br><span class="line">	at com.gbasedbt.util.IfxErrMsg.getSQLException(IfxErrMsg.java:<span class="number">408</span>)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqli.a(IfxSqli.java:<span class="number">3572</span>)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqli.D(IfxSqli.java:<span class="number">3852</span>)</span><br><span class="line">	at com.gbasedbt.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:<span class="number">273</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">        <span class="comment">//servletRequest.setCharacterEncoding(&quot;UTF-8&quot;);</span></span><br><span class="line">        servletRequest.setCharacterEncoding(<span class="string">&quot;GB18030-2000&quot;</span>);</span><br><span class="line">和</span><br><span class="line">    	<span class="comment">//NEWCODESET=utf8,GB18030-2000,5488</span></span><br><span class="line">   		NEWCODESET=GB18030-<span class="number">2000</span>,utf8,<span class="number">57372</span></span><br><span class="line">需要对应。</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>这种校验是好事， 如果你不校验，那么你的数据是什么编码会很乱，且无法维护统一编码。</p>
<p>所以很简单： </p>
<ol>
<li>来啦一个数要存，我设置了NEWCODESET,那么把这个数据从JDK中取到，把他从NEWCODE转成校验字符集给到表存储。</li>
<li>来啦一个要取数，我设置了NEWCODESET, 那么我把你存储的这个字符取出来，把他从校验字符集转换成NEWCODE给到JDK结束。</li>
<li>CLIENT_LOCALE对于jdbc不起作用，可以省略，或保持和DB_LOCLAE一致。</li>
</ol>
<h3 id="jdbc夹带用户名和密码"><a href="#jdbc夹带用户名和密码" class="headerlink" title="jdbc夹带用户名和密码"></a>jdbc夹带用户名和密码</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">String</span> <span class="variable">url</span> <span class="operator">=</span> <span class="string">&quot;jdbc:Informix-sqli://10.127.1.11:8001/testdb:</span></span><br><span class="line"><span class="string">InformixSERVER=servername;user=user;password=password;</span></span><br><span class="line"><span class="string">DB_LOCALE=zh_CN.gb;CLIENT_LOCALE=zh_CN.gb&quot;</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="破坏测试-往jbk库里面插入utf8编码的数据"><a href="#破坏测试-往jbk库里面插入utf8编码的数据" class="headerlink" title="破坏测试-往jbk库里面插入utf8编码的数据"></a>破坏测试-往jbk库里面插入utf8编码的数据</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">//这样可以往jbk库里面插入utf8编码的数据：“汪伟”能插入，“汪”不能插入。</span><br><span class="line"></span><br><span class="line">jdbc:gbasedbt-sqli://1.1.1.3:9031/gbk:GBASEDBTSERVER=g31;DB_LOCALE=zh_CN.GB18030-2000;CLIENT_LOCALE=zh_CN.GB18030-2000;NEWCODESET=utf8,GB18030-2000,5488;IFX_LOCK_MODE_WAIT=30</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>







 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/05/10/%E5%85%A5%E9%97%A8-JAVA%E4%B9%8B%E8%B7%AF/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E5%9F%BA%E7%A1%80/" rel="tag">基础</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/05/10/GBase8s%E5%A4%87%E4%BB%BD%E7%AD%96%E7%95%A5%E5%A4%87%E4%BB%BD%E7%A8%8B%E5%BA%8F%E9%83%A8%E7%BD%B2%E6%9F%A5%E7%9C%8B/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            GBase8s备份策略备份程序部署查看
          
        </div>
      </a>
    
    
      <a href="/2023/05/10/%E5%85%A5%E9%97%A8-Golang%E4%B9%8B%E8%B7%AF/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">入门-Golang之路</div>
      </a>
    
  </nav>

  
   
  
    
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>